package xyz.wbsite.dbtool.javafx.ctrl;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TableView;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import xyz.wbsite.dbtool.javafx.JavaFxApplication;
import xyz.wbsite.dbtool.javafx.manger.ManagerFactory;
import xyz.wbsite.dbtool.javafx.manger.ProjectManager;
import xyz.wbsite.dbtool.javafx.po.Project;
import xyz.wbsite.dbtool.javafx.tool.Dialog;

import java.io.File;

public class MainController {
    private ProjectManager projectManager = ManagerFactory.getProjectManager();

    @FXML
    private TreeView dbtree;
    @FXML
    private Pane detail;
    @FXML
    private TableView fields;
    @FXML
    private Button add;
    @FXML
    private Button sub;
    @FXML
    private CheckBox mcdx;
    @FXML
    private BorderPane borderPane;
    @FXML
    private SplitPane splitPane;

    private JavaFxApplication main;

    public ProjectManager getProjectManager() {
        return projectManager;
    }

    public void setProjectManager(ProjectManager projectManager) {
        this.projectManager = projectManager;
    }

    public JavaFxApplication getMain() {
        return main;
    }

    public void setMain(JavaFxApplication main) {
        this.main = main;
    }

    public Button getAdd() {
        return add;
    }

    public void setAdd(Button add) {
        this.add = add;
    }

    public Button getSub() {
        return sub;
    }

    public void setSub(Button sub) {
        this.sub = sub;
    }

    public TableView getFields() {
        return fields;
    }

    public void setFields(TableView fields) {
        this.fields = fields;
    }

    public TreeView getDbtree() {
        return dbtree;
    }

    public void setDbtree(TreeView dbtree) {
        this.dbtree = dbtree;
    }

    public Pane getDetail() {
        return detail;
    }

    public void setDetail(Pane detail) {
        this.detail = detail;
    }

    public BorderPane getBorderPane() {
        return borderPane;
    }

    public void setBorderPane(BorderPane borderPane) {
        this.borderPane = borderPane;
    }

    public SplitPane getSplitPane() {
        return splitPane;
    }

    public void setSplitPane(SplitPane splitPane) {
        this.splitPane = splitPane;
    }

    @FXML
    public void modelOpen(ActionEvent actionEvent) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("选择模型文件");
        fileChooser.setInitialDirectory(projectManager.getPath());

        fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("模型文件", "*.xml", "*.XML"));

        Stage stage = new Stage();
        File file = fileChooser.showOpenDialog(stage);

        if (file != null && file.exists()) {
            main.initProjectTree(projectManager.invalidate(file));//加载模型树
            main.loadProject();//显示项目面板
        }
    }

    @FXML
    public void modelSave(ActionEvent actionEvent) {
        if (projectManager.getPath() == null || "".equals(projectManager.getPath())) {
            Dialog.showDirectoryChooser(null, new Dialog.DirectoryCall() {
                @Override
                public void call(File result) {
                    projectManager.setPath(result.getAbsoluteFile());
                }
            });
        }
        projectManager.save();
    }

    @FXML
    public void modelSaveAs(ActionEvent actionEvent) {
        Dialog.showDirectoryChooser(null, new Dialog.DirectoryCall() {
            @Override
            public void call(File result) {
                projectManager.save(result);
            }
        });
    }

    @FXML
    public void modelCreate(ActionEvent actionEvent) {
        main.initProjectTree(new Project());
        main.loadProject();
    }

    @FXML
    public void modelDelete(ActionEvent actionEvent) {
        Dialog.showYesNo("", "确认删除当前模型?", new Dialog.ConfirmCall() {
            @Override
            public void call(boolean result) {
                if (result) {
                    projectManager.delete();
                    main.initProjectTree(projectManager.invalidate());
                    main.loadProject();
                }
            }
        });
    }

    @FXML
    public void modelImport(ActionEvent actionEvent) {
        Dialog.showDBConnectInput();
    }

    @FXML
    public void modelGenerate(ActionEvent actionEvent) {
        this.modelSave(null);
        if (projectManager.doCheck()) {
            Dialog.showDirectoryChooser(projectManager.getPath(), new Dialog.DirectoryCall() {
                @Override
                public void call(File result) {
                    System.out.println("生成目录:" + result.getAbsolutePath());
                    projectManager.generate(ManagerFactory.getProjectManager().getProject(), result);
                }
            });
        }
    }

    @FXML
    public void generateVue(ActionEvent actionEvent) {
        Dialog.showVue();
    }

    @FXML
    public void generateApi(ActionEvent actionEvent) {
        Dialog.showApi();
    }

    @FXML
    public void generateDoc(ActionEvent actionEvent) {
        Dialog.showDoc();
    }

    @FXML
    public void generateAndroid(ActionEvent actionEvent) {
        Dialog.showAndroid();
    }

    @FXML
    public void exit(ActionEvent actionEvent) {
        modelSave(actionEvent);
        System.exit(0);
    }

    public CheckBox getMcdx() {
        return mcdx;
    }

    public void setMcdx(CheckBox mcdx) {
        this.mcdx = mcdx;
    }
}
